package cn.csc.module.pay.service.bank;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.math.BigInteger;
import java.util.*;
import cn.csc.module.pay.controller.admin.bank.vo.*;
import cn.csc.module.pay.dal.dataobject.bank.PayBankDO;
import cn.csc.framework.common.pojo.PageResult;

import cn.csc.module.pay.convert.bank.PayBankConvert;
import cn.csc.module.pay.dal.mysql.bank.PayBankMapper;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.pay.enums.ErrorCodeConstants.*;

/**
 * 资金账户绑定的银行卡 Service 实现类
 *
 * @author 华南城
 */
@Service
@Validated
public class PayBankServiceImpl implements PayBankService {

    @Resource
    private PayBankMapper bankMapper;

    @Override
    public Long createBank(PayBankCreateReqVO createReqVO) {
        PayBankDO checkDO= bankMapper.selectByAppUserId(createReqVO.getMerchantId(),createReqVO.getAppUserId());
        Long id=null;
        if(checkDO!=null){
            // 更新
            PayBankDO updateObj = PayBankConvert.INSTANCE.convert(createReqVO);
            updateObj.setId(checkDO.getId());
            bankMapper.updateById(updateObj);
            id=updateObj.getId();
        }else {
            // 插入
            PayBankDO bank = PayBankConvert.INSTANCE.convert(createReqVO);
            bankMapper.insert(bank);
            id=bank.getId();
        }
        // 返回
        return id;
    }

    @Override
    public void updateBank(PayBankUpdateReqVO updateReqVO) {
        // 校验存在
        validateBankExists(updateReqVO.getId());
        // 更新
        PayBankDO updateObj = PayBankConvert.INSTANCE.convert(updateReqVO);
        bankMapper.updateById(updateObj);
    }
    @Override
    public void deleteBank(Long id) {
        // 校验存在
        validateBankExists(id);
        // 删除
        bankMapper.deleteById(id);
    }

    private void validateBankExists(Long id) {
        if (bankMapper.selectById(id) == null) {
            throw exception(BANK_NOT_EXISTS);
        }
    }

    @Override
    public PayBankDO getBank(Long id) {
        return bankMapper.selectById(id);
    }

    @Override
    public PayBankRespApiVO getBankByAppUserId(BigInteger merchantId, String appUserId, int state){
        return bankMapper.selectByAppUserId(merchantId, appUserId,state);
    }


    @Override
    public List<PayBankDO> getBankList(Collection<Long> ids) {
        return bankMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PayBankDO> getBankPage(PayBankPageReqVO pageReqVO) {
        return bankMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PayBankDO> getBankList(PayBankExportReqVO exportReqVO) {
        return bankMapper.selectList(exportReqVO);
    }

    @Override
    public int updateState(PayBankDO payBankDO) {
        return bankMapper.updateState(payBankDO);
    }

    @Override
    public int updateStateById(PayBankDO payBankDO) {
        return bankMapper.updateStateById(payBankDO);
    }
}
